const express = require("express")
const {
    find
} = require("../mod/test")
const Test = require("../mod/test")
const test = express.Router() //创建路由

//练习中心获取试题
test.get("/getTests", async (req, res) => {
    //分页查询所需的试题
    //所需参数 年级，科目，试题类型(错题，收藏)，当前页，每页多少题
    let num = req.query.num-0
    let pageSize = req.query.pageSize-0
    skip = pageSize * (num - 1) //跳过多少条，
    let data = await Test.find({
        test_grade: req.query.test_grade,
        test_project: req.query.test_project,
    }).skip(skip).limit(pageSize)
    let total=await Test.find({
        test_grade: req.query.test_grade,
        test_project: req.query.test_project,
    }).count()

    if (!data) {
        return res.status(500).json({
            message: "获取失败，请重试"
        })
    }

    res.status(200).json({
        message: "获取ok",
        data,
        total
    })
})

//上传新的试题
test.post("/newTest", async (req, res) => {

    if (req.body.test_type == 2) {
        req.body.test_answer = req.body.test_answer.toString()
    }
    console.log(req.body)
    let data = await new Test(req.body).save()
    // console.log(data)

    if (data) {
        return res.status(200).json({
            message: "上传试题成功"
        })
    }
    res.status(201).json({
        message: "上传试题失败，请重试"
    })

})

module.exports = test